package com.beiding.jsonanalyzer.querylink;

import com.beiding.jsonanalyzer.flow.BranchNode;
import com.beiding.jsonanalyzer.flow.ValueNode;
import com.beiding.jsonanalyzer.flow.flowevents.DataNodeEvent;

import java.util.function.Function;

/**
 * 节点转换
 */
public class TransformQueryLinkNode<W extends ValueNode> extends ComputeNode<W> {

    private Function<Object, Object> function;

    public TransformQueryLinkNode(QueryLinkNode<W> pre, Function<Object, Object> nodeTransform) {
        super(pre);
        function = nodeTransform;
    }


    @Override
    protected void bindByComputeNode(BranchNode<W> preResult, BranchNode<W> newBranchNode) {

        ComputeNodeUtils.bindControlEvent(preResult, newBranchNode);


        preResult.onDataEvent(DataNodeEvent.eventType(), event -> {
            W w = newBranchNode.getBranch().getValueNodeFactory().create();
            w.set(function.apply(event.getData().get()));

            newBranchNode.signDataEvent(new DataNodeEvent<>(
                    w
            ));
        });

    }

}
